#pragma once
#include "Log.hpp"
#include "Sock.hpp"
#include <functional>
#include<iostream>
#include <pthread.h>
#include <vector>

namespace ns_tcpserver
{
    using func_t = std::function<void(int)>;
    class TcpServer;
    class ThreadData 
    {
    public:
        ThreadData(){
            
        }
    public:
        TcpServer* _server;
        int _sock;
    }
    class TcpServer 
    {
    public:
        TcpServer(const uint16_t& port,const std::string& ip)
        {
            _listensock=_sock.Socket();
            _sock.Bind(_listensock, port, ip);
            _sock.Listen(_listensock);
        }
        void BindHandler(func_t func)
        {
            _func.push_back(func);
        }
        void Execute(int sock)
        {
            for(auto& iter:_func){
                iter(sock);
            }
        }
        void Start()
        {
            while(true)
            {
                std::string client_ip;
                uint16_t client_port;
                int sock=_sock.Accept(_listensock, &client_port, &client_ip);
                if(sock == -1){
                    continue;
                }
                LogMessage(NORMAL, "create new link : %d", sock);
                pthread_t tid;
                //ThreadData* td=new ThreadData()
            }
        }
    private:
        int _listensock;
        Sock _sock;
        std::vector<func_t>_func;
    };
}
